<?php
/**
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

namespace App\Admin\Extensions\Export;


use App\Http\Controllers\Admin\Business\Repository\Combo;
use App\Member;
use App\Models\ComboExchange;
use Carbon\Carbon;
use Encore\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Facades\Excel;


class MemberExchangeExport extends  AbstractExporter
{
    public function export()
    {
        $combo= Combo::pluck('title','id')->toArray();
        Excel::create('用户激活码'.Carbon::now(), function($excel)use($combo) {

            $excel->sheet('Sheetname', function($sheet)use($combo) {
                $rows1=collect([['用户昵称','激活码','套餐','时间']]);
                $rows = collect($this->getData())->map(function ($item)use($combo) {
                    return [
                        Member::find($item['member_id'])->nickname ?? '',
                        ComboExchange::find($item['exchange_id'])->code ?? '',
                        $combo[ ComboExchange::find($item['exchange_id'])->combo_id] ?? '',
                        $item['created_at']
                    ];
                });
                $sheet->rows($rows1);
                $sheet->rows($rows);
            });

        })->export('xls');
    }
}